package com.qys.livesMall.event.listener;

import com.qys.livesMall.app.user.event.UserTeamEvent;
import com.qys.livesMall.app.user.model.dto.sys.UserTeamEventDTO;
import com.qys.livesMall.app.user.model.entity.UserShopStatistics;
import com.qys.livesMall.app.user.model.entity.UserStatisticsTeam;
import com.qys.livesMall.app.user.model.entity.UserStatisticsInfo;
import com.qys.livesMall.app.user.service.UserShopStatisticsService;
import com.qys.livesMall.app.user.service.UserStatisticsTeamService;
import com.qys.livesMall.app.user.service.UserStatisticsInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

@Slf4j
@RequiredArgsConstructor
public class UserTeamListener {

    private final UserStatisticsTeamService userStatisticsTeamService;
    private final UserShopStatisticsService userShopStatisticsService;
    private final UserStatisticsInfoService userStatisticsInfoService;
    @Async
    @Order
    @EventListener(UserTeamEvent.class)
    public void saveUserTeamInfo(UserTeamEvent userTeamEvent){
        log.info("===========用户渠道添加监听===========");
        UserTeamEventDTO userTeamEventDTO= (UserTeamEventDTO) userTeamEvent.getSource();
        //新增团队
        UserStatisticsTeam userStatisticsTeam =new UserStatisticsTeam();
        userStatisticsTeam.setNum(0);
        userStatisticsTeam.setRegisterNum(0);
        userStatisticsTeam.setTeamId(Long.valueOf(userTeamEventDTO.getId()));
        userStatisticsTeam.setName(userTeamEventDTO.getNickName());
        userStatisticsTeamService.save(userStatisticsTeam);
        //增加用户信息
        UserShopStatistics userShopStatistics=new UserShopStatistics();
        userShopStatistics.setUserId(Long.valueOf(userTeamEventDTO.getId()));
        userShopStatistics.setTeamId(Long.valueOf(userTeamEventDTO.getId()));
        userShopStatistics.setRegisterTime(userTeamEventDTO.getCreateTime());
        userShopStatistics.setAgentAccount(userTeamEventDTO.getUserName());
        userShopStatistics.setStorey(0);
        userShopStatisticsService.save(userShopStatistics);
        UserStatisticsInfo userStatisticsInfo=new UserStatisticsInfo();
        userStatisticsInfo.setUserId(Long.valueOf(userTeamEventDTO.getId()));
        userStatisticsInfoService.save(userStatisticsInfo);
    }
}
